package com.youhong.teethcare.settingpage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.youhong.teethcare.BaseActivity;
import com.youhong.teethcare.R;
import com.youhong.teethcare.adapter.SpinnerAdapter;
import com.youhong.teethcare.customviews.ShSwitchView;
import com.youhong.teethcare.netbeans.OtaHttpReturn;
import com.youhong.teethcare.services.Common;
import com.youhong.teethcare.utils.AppManager;
import com.youhong.teethcare.utils.HttpJsonUtils2;
import com.youhong.teethcare.utils.ToastUtil;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

public class FeedbackActivity extends BaseActivity implements View.OnClickListener
        , ShSwitchView.OnSwitchStateChangeListener {

    Button btn_post;
    EditText et_email, et_text, et_name;
    ImageView iv_back;
    Spinner sp;
    ShSwitchView switchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        initStatusBar(getResources().getColor(R.color.pageBackground),true);

        AppManager.getInstance().addActivity(this);
        getViews();
        network_thread.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        network_thread.interrupt();
    }

    Handler network_handler;

    HandlerThread network_thread = new HandlerThread("network") {

        @Override
        protected void onLooperPrepared() {

            network_handler = new Handler() {

                @Override
                public void handleMessage(Message msg) {
                    List<NameValuePair> params = new ArrayList<>();

                    String[] str = getResources().getStringArray(R.array.feedback_topics);

                    params.add(new BasicNameValuePair("appName", "Teethcare"));
                    params.add(new BasicNameValuePair("osType", "Android"));
                    params.add(new BasicNameValuePair("name", et_name.getText().toString()));
                    params.add(new BasicNameValuePair("email", et_email.getText().toString()));
                    params.add(new BasicNameValuePair("topic", str[sp.getSelectedItemPosition()]));
                    params.add(new BasicNameValuePair("content", et_text.getText().toString()));
                    params.add(new BasicNameValuePair("deviceType", "2058"));

                    OtaHttpReturn mReturn = HttpJsonUtils2.doPostFormEncoded_Sync(HttpJsonUtils2.URL_FEEDBACK
                            , params, OtaHttpReturn.class);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            return_feedback(mReturn);
                        }
                    });
                }
            };
        }
    };

    private void return_feedback(OtaHttpReturn mReturn) {

        if (mReturn == null) {


        } else if (mReturn.getMsgCode() == 1) {
            Intent intent = new Intent(FeedbackActivity.this, FeedbackSuccessActivity.class);
            startActivityForResult(intent, REQ_SUCCESS);
        }

    }

    private static int REQ_SUCCESS = 0x2;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == REQ_SUCCESS) {
            et_name.setText("");
            et_email.setText("");
            et_text.setText("");
            sp.setSelection(0);
            this.finish();
        }
        super.onActivityResult(requestCode, resultCode, data);

    }
    private void getViews() {

        btn_post = findViewById(R.id.bt_advice);
        btn_post.setOnClickListener(this);
        switchView = findViewById(R.id.feedback_switchView);

        switchView.setOn(Common.userInfo.getCollectLog());
        switchView.setOnSwitchStateChangeListener(this);
        et_name = findViewById(R.id.et_advice_name);
        et_email = findViewById(R.id.et_advice_email);
        et_text = findViewById(R.id.et_advice_text);
        iv_back = findViewById(R.id.aboutUs_iv_back);

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppManager.getInstance().killTopActivity();
            }
        });
        sp = findViewById(R.id.feedback_spinner);
        SpinnerAdapter adapter
                = new SpinnerAdapter(getApplicationContext()
                , R.layout.sp_item,
                getResources().getStringArray(R.array.feedback_topics));
        sp.setAdapter(adapter);
        sp.setSelection(0);
    }

    @Override
    public void onClick(View v) {

        if (v == btn_post) {

            if (et_text.length() == 0) {

                ToastUtil.showShortToast(this, R.string.feedback_str1);

            } else if (et_email.length() == 0) {

                ToastUtil.showShortToast(this, R.string.feedback_str2);

            } else if (et_name.length() == 0) {

                ToastUtil.showShortToast(this, R.string.feedback_str13);

            } else if (sp.getSelectedItemPosition() == 3) {

                ToastUtil.showShortToast(this, R.string.feedback_str14);

            } else if (!et_email.getText().toString().contains("@")) {

                ToastUtil.showShortToast(this, R.string.feedback_str3);

            } else {
                network_handler.handleMessage(network_handler.obtainMessage());
            }
        }
    }

    @Override
    public void onSwitchStateChange(View view, boolean isOn) {

        Common.userInfo.setCollectLog(isOn);
        Common.userInfo.SyncToLocal(getApplicationContext());

    }
}
